﻿//初始化
var g_url = "";
$(document).ready(function () {   
   Page_Load();  
});

function Page_Load() {
    //设置dg属性
    $("#dg").treegrid({
        url: "../Menu/GetAllMenus",
        loadMsg: "加载数据，请稍后...",
        toolbar: "#toolbar",
        idField: "ID",
        treeField: "Title",
        rownumbers: "true",
        animate: true,
        fitColumns: "true",
        showFooter: false,
        rowStyler: function (row) {
            if (row.persons > 1) {
                return 'background:#AAD684;color:#fff';
            }
        },
        frozenColumns: [[                   
	                { field:'Title',title:'标题', width: 200,
		                formatter:function(value){
		                	return '<span style="color:red">'+value+'</span>';
		                }
	                }
				]],
		columns:[[
			{field:'Url',title:'链接',width:150},
			{field:'Sequence',title:'顺序',width:50,rowspan:2},
			{field:'IsShow',title:'是否显示',width:50,rowspan:2},
            {field:'CreateTime',title:'创建时间',width:100,rowspan:2}
		]]
       
    });

    //绑定事件
    $("#lbtnAddRoot").click(lbtnAddRoot_Click);
    $("#lbtnAdd").click(lbtnAdd_Click);
    $("#lbtnEdit").click(lbtnEdit_Click);
    $("#lbtnRemove").click(lbtnRemove_Click);
    $("#lbtnSave").click(lbtnSave_Click);
    $("#lbtnQuery").click(lbtnQuery_click);
}

function lbtnAddRoot_Click() {
  
    //显示添加账号对话框
    $("#dlg").dialog({ modal: true, shadow: true });  //模态窗口
    $("#dlg").dialog("open").dialog("setTitle", "添加根菜单");
    $("#fm").form("clear");
    //请求新增
    $("#txtSequence").val(0);
    $("#txtUrl").val("#");
    $("#txtParentID").val("root");
    $("#txtIsShowYes").attr("checked", true);
    g_url = "../Menu/MenuRootCreate"; 
}

function lbtnAdd_Click() {
    var row = $('#dg').treegrid('getSelected');     
    if (row) {
        //显示添加账号对话框
        $("#dlg").dialog({ modal: true, shadow: true });  //模态窗口
        $("#dlg").dialog("open").dialog("setTitle", "添加子菜单");
        $("#fm").form("clear");
        //请求新增
        $("#txtSequence").val(0);
        $("#txtUrl").val("#");
        $("#txtParentID").val(row.ID);
        $("#txtIsShowYes").attr("checked", true);
        g_url = "../Menu/MenuCreate";
    }
    else {       
        $.messager.show({
            title: '系统提示',
            msg: "请选择一行记录",
            showSpeed:400
        });
    }      
 }
 

function lbtnEdit_Click() {
    var row = $('#dg').treegrid('getSelected');
    if (row) {
        $("#dlg").dialog({ modal: true, shadow: true });  //模态窗口
        $("#dlg").dialog("open").dialog("setTitle", "修改角色");
        $('#fm').form('load', row);
        $("#txtMenuID").val(row.ID);
        $("#txtMenuID").attr("disabled", "disabled");
        $("#txtParentID").val(row._parentId);
        $("#txtParentID").attr("disabled", "disabled");
        g_url = "../Menu/MenuEdit";
    }
    else {
        $.messager.show({
            title: '系统提示',
            msg: "请选择一行记录"          
        });
    }

}
function lbtnRemove_Click(){

    //删除一条或多条记录
    var row = $("#dg").treegrid("getSelected"); //返回一个实体数组集合
    if (!row) {
        $.messager.alert('系统提示', "请选择一行记录", "info"); 
          
        return;
    }
    //确定删除
    $.messager.confirm("系统提示", "你确定要删除?", function (cfm){
        if(cfm){
            $.post("../Menu/Delete", { id : row.ID },
            function (result) {
                if (result.code == 1) {
                    $('#dg').treegrid('reload');    // reload the user data  
                } else {
                    $.messager.show({
                        title: '系统提示',
                        msg: result.message
                    });
                }
            }, "json");
         }
      });
}

function lbtnSave_Click() {
    var v_MenuID = $("#txtMenuID").val();
    var v_Title=$("#txtTitle").val();
    var v_Url = $("#txtUrl").val();
    var v_Sequence = $("#txtSequence").val();
    var v_IsShow = $(":radio:checked").val();
    var v_ParentID =$("#txtParentID").val();
    $.post(g_url, { id: v_MenuID, title: v_Title, url: v_Url, sequence: v_Sequence, isShow: v_IsShow, parentID: v_ParentID },
        function (result) {
            if (result.code == "1") {
                $('#dlg').dialog('close');      // close the dialog  
                $('#dg').treegrid('reload');    // reload the user data  
                //刷新导航折叠菜单
               // LoadMenu();
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result.message
                });
            }
        }, "json");
}

//搜索
function lbtnQuery_click() {
//    v_roleName = $('#txtQueryName').val();
//    $('#dg').treegrid('load', { roleName: v_roleName });

//    $.post('/User/List', { name: 'mikel' }, function (data) {
//        $('#treegrid').treegrid('loadData', data);
//    }, 'json');
}